//index.js
//获取应用实例
const app = getApp();
var template = require('../../utils/tabBarTemplate/tabBarTemplate.js');
var _page = 1;
var userInfo = {};
Page({
  data: {
    dongtai_json:{},//动态分页+列表
    announcement:[],//动态列表
    httpPath: app.globalData.imagePath,
    sessionkey:'',
    comment_status:false,//评论输入框的
    showOrHidden:true,//自定义tab 显示状态
    comment_val:'',//评论内容,
    article_id:'',//动态的id
    _pinglunid:'',//评论内容的id


    userInfo: {}, // 存放用户信息
    resultData: [], // 存放数据
    userStatus: {}, // 存放地理位置
    scrolltop: 20, // 滚动轴TOP
    page: 1, // 页码值
    cz_flag: false, // 控制点赞评论按钮
    cz_right: 0, // 点赞评论定位right
    cz_top: 80, // 点赞评论定位top
    dz_id: null, // 点赞评论ID
    animationData: {},
    animationData1: {}, // 发布按钮下滑动画
    animationData2: {} // 位置按钮下滑动画
  },
  onLoad: function () {

    // 调用函数
    // app.editTabBar(); 
    template.tabbar("tabBar", 0, this)//0表示第一个tabbar
    var that = this;

    //获取登陆状态
    var loginID = wx.getStorageSync("codeId");
    var _token = wx.getStorageSync("token");
    that.setData({
      sessionkey: _token
    })
    if (loginID == "" || _token == ""){
      wx.reLaunch({
        url: '../registered/registered'
      })
    }else{
      wx.showLoading({
        title: '数据加载中'
      })
      //获取个人信息
      wx.request({
        url: app.globalData.httpsPath + '/user/info',
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        data: {
          token: _token
        },
        //请求后台数据成功  
        success: function (res) {
          let _info = res.data.data;
          that.setData({
            userinfo: _info
          })
          userInfo = _info;

        }
      })
      
      that.getArticlelist(that);
    }
  },
  //动态列表
  getArticlelist: function (that){
    var _token = that.data.sessionkey;
    wx.request({
      url: app.globalData.httpsPath + '/notice',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _token
      },
      //请求后台数据成功  
      success: function (res) {
        wx.hideLoading();
        var dongtai_json = res.data.data;
        var dongtai_list = res.data.data.data;
        for (var i in dongtai_list) {
          dongtai_list[i].contents = dongtai_list[i].contents.split('&hc').join('\n');//转换回车字符

          if (dongtai_list[i].pics != null) {
            var picimg = dongtai_list[i].pics.split(",");
            dongtai_list[i].pics = picimg;
          }
        }
        that.setData({
          dongtai_json: dongtai_json,
          announcement: dongtai_list
        })
        console.info(dongtai_list)
      }
    })
  },
  //预览图片
  previewImage: function (event) {
    var that = this;
    var httpurl = that.data.httpPath;

    var imgs = event.currentTarget.dataset.imgs;
    var temp = [];
    for (var index in imgs) {
      temp = temp.concat(httpurl + imgs[index]);
    }
    wx.previewImage({
      current: httpurl + event.currentTarget.dataset.src,
      urls: temp,
    })
  },
  // 用户点击右上角分享
  onShareAppMessage: function (options) {

    if (options.from === 'button') {
      // 来自页面内转发按钮
      console.log(options.target)
    }
    return {
      title:'绿茵圆小程序',
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  //下拉刷新
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var that = this;
    var _token = that.data.sessionkey;
    _page = 1;
    //动态列表
    wx.request({
      url: app.globalData.httpsPath + '/notice?page=' + _page,
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _token
      },
      //请求后台数据成功  
      success: function (res) {
        wx.hideLoading();
        var dongtai_json = res.data.data;
        var dongtai_list = res.data.data.data;
        for (var i in dongtai_list) {
          dongtai_list[i].contents = dongtai_list[i].contents.split('&hc').join('\n');//转换回车字符

          if (dongtai_list[i].pics != null) {
            var picimg = dongtai_list[i].pics.split(",");
            dongtai_list[i].pics = picimg;
          }
        }
        that.setData({
          dongtai_json: dongtai_json,
          announcement: dongtai_list
        })
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
      }
    })
  },
  //页面上拉触底事件的处理函数
  onReachBottom: function () {
    var that = this;
    var _token = that.data.sessionkey;
    _page += 1;
    // 显示加载图标
    wx.showLoading({
      title: '数据加载中',
    })
    wx.request({
      url: app.globalData.httpsPath + '/notice?page=' + _page, // 接口地址
      method: "POST",
      header: {
        'content-type': 'application/json', // 默认值
      },
      data: {
        token: _token
      },
      //请求后台数据成功  
      success: function (res) {

        var _list = res.data.data;
        var oldcourt_list = that.data.announcement;

        if (_list.data.length > 0) {
          for (var i = 0; i < _list.data.length; i++) {
            _list.data[i].contents = _list.data[i].contents.split('&hc').join('\n');//转换回车字符
            oldcourt_list.push(_list.data[i]);
            if (_list.data[i].pics != null) {
              var picimg = _list.data[i].pics.split(",");
              _list.data[i].pics = picimg;
            }
          }
          //将获取的数据保存
          that.setData({
            dongtai_json: _list,
            announcement: oldcourt_list
          })

        } else {
          that.setData({
            promptText: false
          })
        }
        // 隐藏加载框
        wx.hideLoading();
      }
    })

  },
  //显示评论输入框
  pinglunBtn: function (event){
    var _that = this;
    var _article_id = event.currentTarget.dataset.id;
    var _pinglun = event.currentTarget.dataset.newpinlun;
    var _userid = event.currentTarget.dataset.userid;
    if (_userid == userInfo.id){//判断是否自己评论的内容 是的话就显示删除评论
      wx.showModal({
        content: '删除我的评论',
        confirmText: '删除',
        success: function (res) {
          if (res.confirm) {
            //console.log('用户点击确定')
            //删除评论
            wx.request({
              url: app.globalData.httpsPath + '/comment/del',
              method: 'POST',
              header: {
                'content-type': 'application/json'
              },
              data: {
                token: _that.data.sessionkey,
                id: _pinglun,
              },
              //请求后台数据成功  
              success: function (res) {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'succes',
                  duration: 1500,
                  mask: true
                })
                setTimeout(function () {
                  _that.getArticlelist(_that);
                },1500);
              }
            })
          } else if (res.cancel) {
            //console.log('用户点击取消')
          }
        }
      })
    }else{//如果没有评论过就显示评论输入框
      _that.setData({
        comment_status: true,
        showOrHidden: false,
        article_id: _article_id,
        _pinglunid: _pinglun
      })
    }
   
    console.info(_userid)
    
  },
  //隐藏输入框
  hideInput:function(event){
    var _that = this;
    var _val = event.detail.value;
    if (_val != ""){
      _that.setData({
        comment_val: _val
      });
    }else{
      _that.setData({
        comment_status: false,
        showOrHidden: true
      })
    }
    
  },
  //发送评论
  commentSend: function (event){
    
    var _that = this ;
    var _val = event.detail.value.commentInput;
    var _token = _that.data.sessionkey;
    var article_id = _that.data.article_id;
    var _pid = _that.data._pinglunid

    if (_val == ""){
      wx.showToast({
        icon: "none",
        title: '请输入评论内容'
      })
      return false;
    }
    //保存评论
    wx.request({
      url: app.globalData.httpsPath + '/comment/save',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _token,
        msg: _val,
        article_id:article_id,
        pid: _pid,
      },
      //请求后台数据成功  
      success: function (res) {
        _that.setData({
          comment_status: false,
          showOrHidden: true
        })

        _that.getArticlelist(_that);
        
      }
    })
  },

  
})
